package config

//
// Created by GoLand.
// User: wuzhouhai
// Date: 2025/2/20
//
// Description: 配置中心 使用goFrame配置框架
//管理加载所有配置文件
//配置读取
import (
	"context"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/container/gvar"
	"sync"
)

type ConfigMaster struct {
	configMaps       *gmap.StrAnyMap   // 其他配置文件,主要是各服务的配置,可以自定义
	hyperNexusConfig *ConfigHyperNexus // 框架默认配置,位于运行时目录config/hyperNexus.yaml
}

func newInstance() *ConfigMaster {
	c := &ConfigMaster{
		configMaps:       gmap.NewStrAnyMap(true),
		hyperNexusConfig: NewConfigHyperNexus(),
	}
	return c
}

func (c *ConfigMaster) LoadConfig(name string, configFile string) {
	nc := NewHyperNexusConfig(configFile)
	c.configMaps.Set(name, nc)
}

func (c *ConfigMaster) GetHyperNexusValue(ctx context.Context, key string, def ...interface{}) (*gvar.Var, error) {
	return c.hyperNexusConfig.GetValue(ctx, key, def...)
}

func (c *ConfigMaster) MustGetHyperNexusValue(ctx context.Context, key string, def ...interface{}) *gvar.Var {
	return c.hyperNexusConfig.MustGetValue(ctx, key, def...)
}

//func (c *ConfigMaster) GetValue(configName, ctx context.Context, key string, def ...interface{}) (*gvar.Var, error) {
//	return c.hyperNexusConfig.GetValue(ctx, key, def...)
//}
//
//func (c *ConfigMaster) MustGetValue(configName, ctx context.Context, key string, def ...interface{}) *gvar.Var {
//	return c.hyperNexusConfig.MustGetValue(ctx, key, def...)
//}

var instance *ConfigMaster
var once sync.Once

func HyperNexusConfigMaster() *ConfigMaster {
	once.Do(func() {
		instance = newInstance()
	})
	return instance
}
